#pragma once

#include <iostream>
#include <memory.h>
#include <cmath>

class chessboard;

using namespace std;

class chess
{
private:
	int id;//等级
public:
	chess(int i) :id(i) {}

	int get() 
	{ 
		return id; 
	}
	virtual bool judge_move(chessboard& cb, int startx, int starty, int aimx, int aimy) = 0;
	virtual ~chess() {};//虚析构
};


class chessboard {
private:
	chess* c[10][9];
	char chessword[15][4] = { "帅","相","炮","士","車","馬","兵","","卒","马","车","仕","砲","象","将" };//名字
public:
	chessboard() { memset(c, (int)NULL, sizeof(c)); };//把指针初始化为零指针

	void init();
	chess* get(int x, int y) { return c[x][y]; }
	int getid(int x, int y) //获取坐标
	{
		if (c[x][y] != NULL)
			return c[x][y]->get();
		return 0;
	}
	void show();
	void play();
	bool move(int startx, int starty, int aimx, int aimy);
	~chessboard();//析构函数
	static bool end;//判断结束
	static int player;
};

bool chessboard::end = true;
int chessboard::player = -1;

bool chessboard::move(int startx, int starty, int aimx, int aimy) {
	if (startx >= 0 && startx < 10 && starty >= 0 && starty < 9//初步判断传入的点是否符合规则
		&& aimx >= 0 && aimx < 10 && aimy >= 0 && aimy < 9
		&& getid(startx, starty) && getid(startx, starty) * player>0
		&& c[startx][starty]->judge_move(*this, startx, starty, aimx, aimy)) {
		if (c[aimx][aimy] != NULL) delete c[aimx][aimy];//吃子
		c[aimx][aimy] = c[startx][starty];
		c[startx][starty] = NULL;
		player *= -1;
		return true;
	}
	cout << "走法错误，不符合规则" << endl;
	return false;
}

class horse : public chess {//马的实现
public:
	horse(int i) : chess((i == 0 ? -2 : 2)) {}
	bool judge_move(chessboard& cb, int startx, int starty, int aimx, int aimy) {
		int tempx = aimx - startx, tempy = aimy - starty;
		int sid = cb.getid(startx, starty), aid = cb.getid(aimx, aimy);
		if (sid * aid <= 0 && (tempx * tempx + tempy * tempy == 5) && !cb.get(startx + tempx / 2, starty + tempy / 2))
			return true;
		return false;
	}
};


class soldier : public chess {//兵（卒）的实现
public:
	soldier(int c) : chess((c == 0 ? -1 : 1)) {}
	bool judge_move(chessboard& cb, int startx, int starty, int aimx, int aimy) {
		int tempx = aimx - startx, tempy = aimy - starty;
		int sid = cb.getid(startx, starty), aid = cb.getid(aimx, aimy);
		if (sid * aid <= 0 && sid * tempx <= 0) {
			if (abs(tempx) == 1 && tempy == 0) 
				return true;
			if (abs(tempy) == 1 && tempx == 0)
				if ((startx / 5 == 0 && sid > 0) || (startx / 5 == 1 && sid < 0)) return true;
			return false;
		}
		return false;
	}
};

class general : public chess {//帅（将）的实现
public:
	general(int c) : chess((c == 0 ? -7 : 7)) {}
	bool judge_move(chessboard& cb, int startx, int starty, int aimx, int aimy) {
		int tempx = aimx - startx, tempy = aimy - starty;
		int sid = cb.getid(startx, starty), aid = cb.getid(aimx, aimy);
		if (sid * aid <= 0 && tempy * tempy + tempx * tempx == 1 && aimx % 7 >= 0 && aimx % 7 <= 2 && aimy >= 3 && aimy <= 5)
			return true;
		return false;
	}
	~general() { chessboard::end = false; }
};


class elephant : public chess {//象（相）的实现
public:
	elephant(int c) : chess((c == 0 ? -6 : 6)) {}
	bool judge_move(chessboard& cb, int startx, int starty, int aimx, int aimy) {
		int tempx = aimx - startx, tempy = aimy - starty;
		int sid = cb.getid(startx, starty), aid = cb.getid(aimx, aimy);
		if (sid * aid <= 0 && tempy * tempy + tempx * tempx == 8 && startx / 5 == aimx / 5 && !cb.get(startx + tempx / 2, starty + tempy / 2))
			return true;
		return false;
	}
};

class cannon : public chess {//炮的实现
public:
	cannon(int c) : chess((c == 0 ? -5 : 5)) {}
	bool judge_move(chessboard& cb, int startx, int starty, int aimx, int aimy) {
		int tempx = aimx - startx, tempy = aimy - starty;
		int sid = cb.getid(startx, starty), aid = cb.getid(aimx, aimy);
		if (sid * aid <= 0 && !(tempx && tempy) && (tempx + tempy)) {
			int tot = 0;
			if (tempx != 0) {
				int sign = tempx > 0 ? 1 : -1;
				for (int i = 1; i < abs(tempx); i++)
					if (cb.get(startx + sign * i, starty)) tot++;
			}
			else {
				int sign = tempy > 0 ? 1 : -1;
				for (int i = 1; i < abs(tempy); i++)
					if (cb.get(startx, starty + sign * i)) tot++;
			}
			if (!aid)
			{
				if (!tot) return true;
			}
			else
			{
				if (tot == 1) return true;
			}
		}
		return false;
	}
};


class guard : public chess {//士（仕）的实现
public:
	guard(int c) : chess((c == 0 ? -4 : 4)) {}
	bool judge_move(chessboard& cb, int startx, int starty, int aimx, int aimy) {
		int tempx = aimx - startx, tempy = aimy - starty;
		int sid = cb.getid(startx, starty), aid = cb.getid(aimx, aimy);
		if (sid * aid <= 0 && tempy * tempy + tempx * tempx == 2 && aimx % 7 >= 0 && aimx % 7 <= 2 && aimy >= 3 && aimy <= 5)
			return true;
		return false;
	}
};
class rook : public chess {//车的实现
public:
	rook(int c) : chess((c == 0 ? -3 : 3)) {}
	bool judge_move(chessboard& cb, int startx, int starty, int aimx, int aimy) {
		int tempx = aimx - startx, tempy = aimy - starty;
		int sid = cb.getid(startx, starty), aid = cb.getid(aimx, aimy);
		if (sid * aid <= 0 && !(tempx && tempy) && (tempx + tempy)) {
			if (tempx != 0) {
				int sign = tempx > 0 ? 1 : -1;
				for (int i = 1; i < abs(tempx); i++)
					if (cb.get(startx + sign * i, starty)) return false;
			}
			else {
				int sign = tempy > 0 ? 1 : -1;
				for (int i = 1; i < abs(tempy); i++)
					if (cb.get(startx, starty + sign * i)) return false;
			}
			return true;
		}
		return false;
	}
};

chessboard :: ~chessboard() {//吃
	for (int i = 0; i < 10; i++)
		for (int j = 0; j < 9; j++)
			if (c[i][j] != NULL) {
				delete c[i][j];
				c[i][j] = NULL;
			}
}



